#include <stdio.h>
#include <stdlib.h>

void scheduler(int task[], int n, int system_task[], int user_task[]);

int main(void)
{
    int task[] = {0, 30, 155, 1, 80, 300, 170, 40, 99};

    int n = sizeof(task) / sizeof(int);

    int i;
    for (i = 0; i < n; ++i)
        printf("%d ", task[i]);
    printf("\n");

    int systemTask[n+1], userTask[n+1];

    scheduler(task, n, systemTask, userTask);

    printf("system task schecule: \n");
    for (i = 0; i < n + 1 && systemTask[i] != -1; i ++)
    {
        printf("%d ", systemTask[i]);
    }
    printf("\n");

    printf("user task schecule: \n");
    for (i = 0; i < n + 1 && userTask[i] != -1; i ++)
    {
        printf("%d ", userTask[i]);
    }
    printf("\n");

    return 0;
}

void scheduler(int task[], int n, int system_task[], int user_task[])
{
    int task_index[n];

    int i, j;
    int k;

    for (i = 0; i < n; i ++)
        task_index[i] = i;

    for (i = 0; i < n - 1; i ++)
    {
        for (j = 0; j < n - i - 1; j ++)
        {
            if (task[j] > task[j+1])
            {
                k = *(task + j);
                *(task + j) = *(task + j + 1);
                *(task + j + 1) = k;
                k = *(task_index + j);
                *(task_index + j) = *(task_index + j + 1);
                *(task_index + j + 1) = k;
            }
        }
    }

    int r = 0, t = 0;

    for (i = 0; i < n; i ++)
    {
        if (task[i] < 50)
        {
            system_task[r++] = task_index[i];
        }
        else if(task[i] <= 255)
        {
            user_task[t++] = task_index[i];
        }
    }
    system_task[r] = -1;
    user_task[t] = -1;
}
